/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.bobo.main;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

/**
 *
 * @author leandro
 */
public class GameScreen extends GameCanvas implements Runnable {

    private Bobo bobo;
    private boolean running = true;
    private Image ground;
    private Image ladder;
    private Image platform;

    private int cps=0;
    private int cyclesThisSecond=0;
    private long lastCPSTime=0;private static final int MAX_CPS = 30;
    private static final int MS_PER_FRAME = 1000 / MAX_CPS;




    public GameScreen(){
        super(false);
        bobo = Bobo.getInstance();
        ground = Image.createImage(getWidth(),20);
        platform = Image.createImage(30,30);
        ladder = Image.createImage(30, getHeight()/2);
        Thread t = new Thread(this);
        t.start();
    }

    public void paint(Graphics g){
        g.setColor(0, 0, 0);
        g.fillRect(0, 0, getWidth(), getHeight());

        ground.getGraphics().setColor(255,255,255);
        ground.getGraphics().fillRect(0, getHeight()/2,getWidth()/2,20);
        platform.getGraphics().fillRect(getWidth()/2, getHeight()/4, 30, 30);
        g.drawImage(platform, getWidth()/2, getHeight()/4, 0);
        g.drawImage(ground, 0, getHeight()/2,0);

        bobo.paint(g);

    }

  /* 
    protected void keyPressed(int keycode){

        if(keycode == KEY_NUM4){
            bobo.setStoped(false);
            bobo.setDirection(Sprite.TRANS_MIRROR);
            bobo.setFrameSequence(bobo.getRunningSeq());
        }
        if(keycode == KEY_NUM6){
            bobo.setStoped(false);
            bobo.setDirection(Sprite.TRANS_NONE);
            bobo.setFrameSequence(bobo.getRunningSeq());
        }
        if(keycode == KEY_NUM2){
            bobo.startJump(System.currentTimeMillis());
        }
    }*/

   /* protected void keyReleased(int keycode){
       switch(keycode){
           case KEY_NUM4:
           case KEY_NUM6: bobo.setStoped(true);
                            break;
           case KEY_NUM2:bobo.stopJump(System.currentTimeMillis());
                      //   bobo.setFrameSequence(bobo.getJumpingSeq());
                          break;
           }
    }*/

    public void run(){
        Graphics g = getGraphics();

        while(running){

             doAction();
             bobo.update(System.currentTimeMillis());

             if(collideWith(bobo.getRefPixelX()-bobo.getWidth()/2, bobo.getRefPixelY()+bobo.getHeight()/2)){
                bobo.setInTheAir(false);
                bobo.setPosition(bobo.getX(),getHeight()/2 - 33);
             }

            //if(platform()){
               // bobo.setInTheAir(false);
              //  bobo.setPosition(bobo.getX(),getHeight()/4 - 33);
            //}

      
            paint(g);
            g.setColor(255, 255, 255);

             try{
                //Thread.sleep(MS_PER_FRAME - timeSinceStart);
                 Thread.sleep(120);
             }catch(Exception ex){}

//            g.drawString("kState"+(key & KEY_NUM9)+" "+KEY_NUM6,0, 0, 0);


            flushGraphics();

        }
    }

    public boolean collideWith(int x, int y){
        if(y > (getHeight()/2)-5)
            return true;
        return false;
    }

    public boolean platform(){

        int x = getWidth()/2;
        int y = getHeight()/4;
        int boboy = bobo.getY()+bobo.getHeight();
        int bobox = 0;

        if((boboy <= y && boboy <=x+30))
            return true;
        return false;
    }
    
    public void doAction(){
        int keyState = getKeyStates();
        
        if((keyState & LEFT_PRESSED) != 0){
            bobo.setStoped(false);
            bobo.setDirection(Sprite.TRANS_MIRROR);
            bobo.setFrameSequence(bobo.getRunningSeq());
        }else{
            if((keyState & RIGHT_PRESSED) != 0){
                bobo.setStoped(false);
                bobo.setDirection(Sprite.TRANS_NONE);
                bobo.setFrameSequence(bobo.getRunningSeq());
            }else{
                if(((keyState & LEFT_PRESSED) == 0) || ((keyState & RIGHT_PRESSED) == 0)){
                    bobo.setStoped(true);
                }
            }
        }
      //  if((keyState & UP_PRESSED) != 0){
       //     bobo.startJump(System.currentTimeMillis());
       // }
        
       // if(((keyState & LEFT_PRESSED) == 0) || ((keyState & RIGHT_PRESSED) == 0)){
        //    bobo.setStoped(true);
       // }

      //  if((keyState & UP_PRESSED) == 0){
           // bobo.stopJump(System.currentTimeMillis());
      //  }
        
        
    }


}
